package com.sunfeng.n6;

import lombok.extern.slf4j.Slf4j;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 测试延时
 *
 * @author sunfeng
 * @date 2021/03/03 13:53
 **/
@Slf4j(topic = "c.TestTimer3")
public class TestTimer3 {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
        log.debug("start");
//        pool.scheduleAtFixedRate(()->{
//                log.debug("aaaaaaaaaaaaaaaaaaa");
//            try {
//                TimeUnit.SECONDS.sleep(2);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        },1,1, TimeUnit.SECONDS);

        pool.scheduleWithFixedDelay(()->{
            log.debug("bbbbbbbbbbbbbbbbbbbbbb");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //delay 是从上次时间结束开始计算
        },1,1, TimeUnit.SECONDS);
    }
}
